package a07_二叉树;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * a25_二叉搜索树中的众数
 * </p>
 *
 * count
 * abccdddef
 *
 * prevNode
 * maxCount
 * count
 *
 * @author flyduck
 * @since 2024/12/23
 */
public class a25_二叉搜索树中的众数复习1 {

    public static void main(String[] args) {
//        TreeNode treeNode = new TreeNode(1,null,new TreeNode(2, new TreeNode(2), null));
        TreeNode treeNode = new TreeNode(0);

        a25_二叉搜索树中的众数复习1 test = new a25_二叉搜索树中的众数复习1();
        test.findMode(treeNode);
    }

    public int[] findMode(TreeNode root) {
        traversal(root);
        return result.stream().mapToInt(i -> i).toArray();
    }

    public int maxCount = Integer.MIN_VALUE;
    public int count = 0;
    public List<Integer> result = new ArrayList<>();
    public TreeNode prevNode;

    public void traversal(TreeNode currentNode){
        if (currentNode == null) {
            return;
        }

        traversal(currentNode.left);

        if (prevNode == null) {
            count = 1;
        }else {
            if(currentNode.val == prevNode.val){
                count++;
            }else {
                count = 1;
            }
        }
        if(count == maxCount){
            result.add(currentNode.val);
        }else if(count > maxCount){
            maxCount = count;
            result.clear();
            result.add(currentNode.val);
        }
        prevNode = currentNode;

        traversal(currentNode.right);
    }


}
